package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dto.TreenityDto;
import com.zhihuishu.micro.course.service.TreenityBatchRedisService;
import com.zhihuishu.micro.course.service.TreenityRedisService;
import com.zhihuishu.toolkit.jedis.JedisHelper;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @author Jiangli
 * @date 2016/10/20 9:33
 * @see TreenityRedisService
 */
public abstract class BaseTreenityBatchServiceRedisImpl<DTO extends TreenityDto> extends BaseTreenityServiceRedisImpl<DTO> implements TreenityBatchRedisService<DTO> {

    @Override
    public void deleteMulti(final List<Integer> dtoIds, long deleteUserId) {
        jedisExecutor.execute( new JedisHelper.Handler() {
            @Override
            public void handle(Jedis jedis) {
                for (Integer dtoId : dtoIds) {
                    doDeleteDto(jedis, dtoId);
                }
            }
        });
    }

    @Override
    public void deleteRelationShip(final List<DTO> dtos) {
        jedisExecutor.execute( new JedisHelper.Handler() {
            @Override
            public void handle(Jedis jedis) {
                for (DTO dto : dtos) {
                    if(!StringUtils.isEmpty(dto.getOneToManyId())){
                        doDeleteRelationShip(jedis,dto.getOneToManyId(),dto.getId());
                    }
                }
            }
        });
    }

    @Override
    public void addRelationShip(final  List<DTO> dtos) {
        jedisExecutor.execute( new JedisHelper.Handler() {
            @Override
            public void handle(Jedis jedis) {
                for (DTO dto : dtos) {
                    if(!StringUtils.isEmpty(dto.getOneToManyId())){
                        doAddIdInListCache(jedis, dto.getOneToManyId(),dto.getId());
                    }
                }
            }
        });
    }

    @Override
    public void sort(final List<DTO> dbDtoList, long updateUserId) {
        jedisExecutor.execute( new JedisHelper.Handler() {
            @Override
            public void handle(Jedis jedis) {
                for (DTO dto : dbDtoList) {
                    updateObjectCacheOfkey(jedis, dto.getId(), "rank", String.valueOf(dto.getRank()));
                }
            }
        });
    }

    @Override
    public final List<DTO> listByMultiOneToManyKey(List<Long> oneToManyIds) {
        //无需重写
        return null;
    }

    @Override
    public final List<Integer> listDtoIdsByMultiOneToManyKey(List<Long> oneToManyIds) {
        //无需重写
        return null;
    }

    @Override
    public final void deleteMultiByMultiOneToManyKey(List<Long> oneToManyKeys, long deleteUserId) {
        //无需重写
    }

    @Override
    public void updateMulti(final List<DTO> dtos, long updateUserId) {
        jedisExecutor.execute( new JedisHelper.Handler() {
            @Override
            public void handle(Jedis jedis) {
                for (DTO dto : dtos) {
                    updateObjectCache(jedis, dto);
                }
            }
        });
    }
}
